package tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.security.SecureRandom;
import java.util.Properties;

import test.process.StreamGobbler;

public class FileReader {

	String profileFP;

	public FileReader(String profileFP) {
		this.profileFP = profileFP;
	}

	public String readHWPFile(String srcFP, String encoding) {

		String result = "";
		try {
			Properties ApplicationProperties = new Properties();
			ApplicationProperties.loadFromXML(new FileInputStream(profileFP));

			final String hwp2txtFP = ApplicationProperties.getProperty("hwp2txt");
			final String hwp2txt_arg = ApplicationProperties.getProperty("hwp2txt_arg");
			final String tempFolderFP = System.getProperty("java.io.tmpdir");

			SecureRandom random = new SecureRandom();
			// Integer randomNumber;
			String hexTempFileName;

			// SRC TEMP - Create to 8-chars file named temp file
			hexTempFileName = Integer.toHexString(random.nextInt());
			File tempSrcFile = new File(tempFolderFP + "\\" + hexTempFileName);
			FileTools.copyFile(srcFP, tempSrcFile.getPath());

			// DEST TEMP Change to 8-chars file named temp file
			hexTempFileName = Integer.toHexString(random.nextInt());

			String tempSrcFP = tempSrcFile.getPath();
			String tempDstFP = tempFolderFP + hexTempFileName;

			if (!tempSrcFile.exists()) {
				System.err.println(">> readHWPFile >> Failed to create a new temp File ");
			}

			String[] cmd = new String[4];
			cmd[0] = hwp2txtFP;
			cmd[1] = hwp2txt_arg;
			cmd[2] = tempSrcFP;
			cmd[3] = tempDstFP;

			Runtime rt = Runtime.getRuntime();

			System.out.println("running " + hwp2txtFP + " " + hwp2txt_arg + " " + tempSrcFP + " " + tempDstFP);
			Process proc = rt.exec(cmd);

			// any error or output message?
			StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
			StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
			// kick them off
			errorGobbler.start();
			outputGobbler.start();

			// any error???
			int exitVal = proc.waitFor();

			if (exitVal > 1) {
				System.out.println("ExitValue: " + exitVal);
				System.err.println(">> readHWPFile >> Failed to read File " + srcFP);
				result = "";
			} else {
				result = readTextFile(tempDstFP, encoding);
			}

			File tempDstFile = new File(tempDstFP);
			tempDstFile.delete();
			tempSrcFile.delete();

		} catch (Throwable t) {
			t.printStackTrace();
		}
		return result;
	}

	private String readTextFile(String srcFP, String charsetIN) {

		StringBuilder content = new StringBuilder("");

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFP), charsetIN));
			String line = null;

			while ((line = reader.readLine()) != null) {
				content.append(line);
			}
			reader.close();
		} catch (Exception ex) {
		}
		return content.toString();
	}
}
